<template>
  <el-dialog
    :title="title"
    :visible.sync="dialogVisible"
    :before-close="handleClose"
    width="500px"
  >
    <el-form
      ref="formDataRef"
      :model="formData"
      :rules="rules"
      label-width="100px"
      style="width: 400px"
      status-icon
    >
      <el-form-item label="名称：" prop="name">
        <el-input v-model="formData.name"></el-input>
      </el-form-item>
      <el-form-item label="备注：" prop="remark">
        <el-input
          v-model="formData.remark"
          type="textarea"
          :autosize="{ minRows: 2, maxRows: 4 }"
          placeholder="请输入内容"
        >
        </el-input>
      </el-form-item>
      <el-form-item align="right">
        <el-button size="mini" @click="handleClose">取 消</el-button>
        <el-button size="mini" type="primary" @click="submitForm('formData')"
          >确 定
        </el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
import { ApiPostRole, ApiPutRoleEdit } from '@/api/role'
export default {
  props: {
    dialogVisible: { //弹出隐藏
      type: Boolean,
      default: false
    },
    title: { // 标题
      type: String,
      default: ''
    },
    formData: { // 表单数据
      type: Object,
      default: {}
    },
    remoteForm: Function, // 关闭弹框
  },
  data () {
    return {
      rules: { // 校 验 规 则
        name: [{ required: true, message: '请输入菜单名称', trigger: 'blur' }]
      }
    }
  },
  methods: {
    handleClose () {
      this.remoteForm()
    },
    submitForm () { // 提交表单数据
      this.$refs['formDataRef'].validate(valid => {
        if (!valid) {
          return false
        }
        this.submitData()
      })
    },
    // 校验成功的方法
    async submitData () {
      let res = null
      // 判断是否存在id
      if (this.formData.id) { // 存在id 编辑操作
        res = await ApiPutRoleEdit(this.formData)
      } else {
        // 新增操作
        res = await ApiPostRole(this.formData)
      }
      if (res.code === 20000) {
        this.$message({
          message: '保存成功', type: 'success'
        })
        this.handleClose()
      } else {
        this.$message({
          message: '保存失败', type: 'error'
        })
      }
    }
  },
}
</script>

<style>
</style>